home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4292 < prev    next >
Encoding:
Text File  |  1996-08-06  |  5.1 KB  |  148 lines

  1. Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
  2. From: jis@ubszh.net.ch (Johnston Ian (by ubsswop))
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: VC++ 4.0 and Templates and the Compiler
  5. Date: 29 Jan 1996 15:35:10 GMT
  6. Organization: UBS
  7. Distribution: world
  8. Message-ID: <4eipfe$hbm@ubszh.fh.zh.ubs.com>
  9. References: <310A7C54.4518@eznet.net>
  10. NNTP-Posting-Host: nol2179.fh.zh.ubs.com
  11.  
  12. In article <310A7C54.4518@eznet.net>, Joe Mihalich <mihalich@eznet.net> writes:
  13. |> Hi,
  14. |> 
  15. |>        Anyone who can answer this question will be the hero of
  16. |>     the year.  I am using Visual C++ 4.0 on both WIN NT and WIN 95.
  17. |> 
  18. |>        I have defined a class template that is derived from the
  19. |>     MFC class CString.  The class template is defined in a DLL.
  20. |>     In order for ALL of the functions to be exported properly,
  21. |>     they have to be instantiated via the compiler.
  22. |> 
  23. |>        Now, not all of the functions are being called from within
  24. |>     the DLL, so the compiler is not instantiating them, which
  25. |>     is not allowing me to call those functions from another
  26. |>     DLL or APP.
  27.  
  28.  
  29. The only thing I can suggest is to try this:
  30.  
  31.             //MyString.h
  32.  
  33.             template <class TYPE> class CMyString : public CString
  34.             {
  35.             TYPE Left(int nStart);
  36.                { return (TYPE)CString::Left(nStart); }
  37.             TYPE Left(int nStart, int nCount);
  38.                { return (TYPE)CString::Left(nStart, nCount); }
  39.             };
  40.  
  41.         template <class TYPE>
  42.         inline CMyString<TYPE>::Left(int nStart)
  43.         { return (TYPE) CString::Left(nStart); }
  44.  
  45.         template <class TYPE>
  46.         inline CMyString<TYPE>::Left(int nStart, int nCount)
  47.         { return (TYPE) CString::Left(nStart, nCount); }
  48.  
  49. and compile with -Ob1.
  50.  
  51. |> 
  52. |>        Microsoft provides 3 ways of solving this problem:
  53. |> 
  54. |>         1) Declare/Define all functions in your class or it's
  55. |>            .h file.  All functions defined there are considered
  56. |>            inline and are automatically instantiated.  For 
  57. |>            example:
  58. |> 
  59. |>            //MyString.h
  60. |> 
  61. |>            template <class TYPE> class CMyString : public CString
  62. |>            {
  63. |>            TYPE Left(int nStart)
  64. |>               { return (TYPE)CString::Left(nStart); }
  65. |>            TYPE Left(int nStart, int nCount)
  66. |>               { return (TYPE)CString::Left(nStart, nCount); }
  67. |>            };
  68. |> 
  69. |>         This is the ideal method for me to use, but it DOES NOT
  70. |>             WORK.  All of my functions are defined in the .h files
  71. |>          cuz there not doing anything but calling the base class
  72. |>         functions.  The only functions that get instantiated are
  73. |>         the ones that are called from within the DLL.  I can verify
  74. |>         this via the .MAP file.
  75. |> 
  76. |>         2) In the source file where the functions are defined, reference
  77. |>            the function somewhere by taking it's address.  I tried this,
  78. |>            but couldn't get it to work either.  This method I probably 
  79. |>            screwed up, cuz I didn't know how to get the address of a function
  80. |>            when there were multiple overloaded versions of the function.
  81. |>            Any suggestions of how to do this?  For example:
  82. |> 
  83. |>            //MyString.h
  84. |> 
  85. |>            template <class TYPE> class CMyString : public CString
  86. |>            {
  87. |>            TYPE Left(int nStart)
  88. |>               { return (TYPE)CString::Left(nStart); }
  89. |>            TYPE Left(int nStart, int nCount)
  90. |>               { return (TYPE)CString::Left(nStart, nCount); }
  91. |>            void Dummy(void);
  92. |>            };
  93. |> 
  94. |>            Now, how do I take the address of either of the LEFT functions?
  95. |> 
  96. |>            //MyString.cpp
  97. |>         
  98. |>            CMyString::Dummy(void)
  99. |>            {
  100. |>           pFunction1 = &this->Left(int nStart);
  101. |>           pFunction2 = &this->Left(int nStart, int nCount);
  102. |>            }
  103. |>            
  104. |>             Is that correct?  If it is, than this method did not work
  105. |>            for me either!
  106.  
  107.     No, the syntax would be
  108.  
  109.         &CMyString<TYPE>::Left(int);
  110.         &CMyString<TYPE>::Left(int, int);
  111.  
  112. |>     
  113. |>         3) Create a dummy function in your base class that calls all
  114. |>            functions.  The dummy function does not need to be
  115. |>            called by anyone.  For Example:
  116. |>         
  117. |>         CMyString::Dummy(void)
  118. |>         {
  119. |>            this->Left(0);
  120. |>            this->Left(0,0);
  121. |>         }
  122. |>            
  123. |>            Unfortunately this is the only method that worked for me.  I consider
  124. |>            this to be an unacceptable solution because it just seems ludicrous
  125. |>            to have to do it this way.
  126. |> 
  127. |> 
  128. |>         If anyone has any ideas on how to make the first solution work, I 
  129. |>         would appreciate it.  I am assuming that there is some stupid 
  130. |>         compiler option that is turning off the expansion of inline
  131. |>         functions or something.  (Bye the way, I tried fooling around with
  132. |>         the compiler option in (C/C++ - Optimizations in the build settings
  133. |>         dialog) called "Inline function expansion".  The possible options
  134. |>         are: disabled, Inline Functions, Any suitable function.  For debug
  135. |>         mode, this option is always set to disabled.  I tried setting it
  136. |>         to either of the other two, but it didn't change anything.  The help
  137. |>         says that this option is just a suggestion to the compiler anyway.
  138. |> 
  139. |>         Any help will be greatly appreciated.
  140. |> 
  141. |>         Thanks,
  142. |>         Joe Mihalich
  143. |> 
  144. |> -- 
  145. |>     Joe Mihalich
  146. |>     Email: mihalich@eznet.net
  147. |>     Voice: 716-482-0089
  148.